//
//  BGCapitalViewController.m
//  bigMall
//
//  Created by ios on 16/7/19.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGCapitalViewController.h"
#import "UIView+FrameExtension.h"
#import "BGCapitalTableViewCell.h"
#import "BGDateResult.h"
#import "BGBGOwnFundListResultModel.h"
#import "BGOwnFundListModel.h"

@interface BGCapitalViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSMutableArray *_dataArr;//数据源
    UITableView *_tableView;
    UIImageView *imageView;
    UILabel *lable;
    int index;
    
}

@end

@implementation BGCapitalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    //     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    /**
     *  Navigation 的设置
     *
     *  @return <#return value description#>
     */
    self.title = @"资产记录";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    
    right.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = right;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createUI];
    
    //初始化了数据源
    [self initDat];
    
}

- (void)loadInter {
    
    [BGDateResult ownFundListWithThisPage:[NSString stringWithFormat:@"%d",index] pageNum:@"10" success:^(NSObject *result) {
        
        BGBGOwnFundListResultModel *model = [[BGBGOwnFundListResultModel alloc] init];
        model = (BGBGOwnFundListResultModel *)result;
        NSArray *listArr = [NSArray arrayWithArray:model.list];
        for (BGOwnFundListModel *model in listArr) {
            [_dataArr addObject:model];
        }
        if (_dataArr.count == 0 && index == 1) {
            
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshingWithNoMoreData];
            
            //判断数据源有无
            imageView.hidden = NO;
            lable.hidden = NO;
            
        }
        if ([model.allPage intValue] > index) {
            _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
        }
//        _dataArr = listArr;
        
        if (listArr.count == 0) {
            [_tableView.footer endRefreshingWithNoMoreData];
            return;
        }
        imageView.hidden = YES;
        lable.hidden = YES;
        [_tableView.footer endRefreshing];
        [_tableView.header endRefreshing];
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)createUI {
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UIAccessibilityTraitNone;
    
    [self.view addSubview:_tableView];
    
    //刷新的加载
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [_tableView.header beginRefreshing];
    
    //
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 70 ) / 2 + 10, (_tableView.height - 109 - 160) / 3, 70, 90)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    imageView.hidden = YES;
    lable.hidden = YES;
    [_tableView addSubview:lable];
    
    [_tableView addSubview:imageView];
    

    
}

- (void)addIndex {
    
    index++;
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}

- (void)initDat {
    
    _dataArr = [NSMutableArray array];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArr.count;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView registerNib:[UINib nibWithNibName:@"BGCapitalTableViewCell" bundle:nil] forCellReuseIdentifier:@"celId"];
    
    BGCapitalTableViewCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    BGOwnFundListModel *model = [[BGOwnFundListModel alloc] init];
    
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        
        NSDate *dat = [NSDate dateWithTimeIntervalSince1970:[model.add_time intValue]];
        NSDateFormatter*df = [[NSDateFormatter alloc]init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *str = [df stringFromDate:dat];
        cel.time.text =  str;
        
        cel.money.text = model.money;
        cel.order.text = model.remark;
        cel.type.text = model.type;
        
    }
  
    return cel;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 127;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [tableView setSeparatorInset:UIEdgeInsetsMake(0,89,0,0)];
        
    }
    
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
